﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WangMiEntity.Order;
using WangMiEntity.GridViewModel;
using Top.Api.Response;
using Top.Api;
using Top.Api.Request;
using WangMiBusiness.BusinessConstant;
using TaoBaoServices;
using Top.Api.Domain;
using WangMiEntity;
using CommonFramework.CommonConstant;

namespace WangMiBusiness.GridViewModel
{
    public class GridViewModelBusiness
    {
        /// <summary>
        /// 获取基本Grid视图模型
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<BaseGridModel> GetBaseGridViewModel(DateTime startTime, DateTime endTime, string session)
        {
            var response = TradeAPI.GetTradesSoldGetResponse(session);

            List<BaseGridModel> baseGridModelList = new List<BaseGridModel>();
            BaseGridModel baseGridModel = new BaseGridModel();

            response.Trades.ForEach(item =>
            {
                if (Convert.ToDateTime(baseGridModel.OrderDate).ToShortDateString() != Convert.ToDateTime(item.Created).ToShortDateString())
                {
                    baseGridModel = new BaseGridModel()
                    {
                        SellerConsignedPartModel = new List<SellerConsignedPartModel>(),
                        TradeBuyerSignedModel = new List<TradeBuyerSignedModel>(),
                        TradeClosedByTaoBaoModel = new List<TradeClosedByTaoBaoModel>(),
                        TradeClosedModel = new List<TradeClosedModel>(),
                        WaitSellerSendGoodsModel = new List<WaitSellerSendGoodsModel>(),
                        TradeFinishedModel = new List<TradeFinishedModel>(),
                        TradeNoCreatePayModel = new List<TradeNoCreatePayModel>(),
                        WaitBuyerConfirmGoodsModel = new List<WaitBuyerConfirmGoodsModel>(),
                        WaitBuyerPayModel = new List<WaitBuyerPayModel>()
                    };

                    baseGridModel.OrderDate = Convert.ToDateTime(item.Created).ToShortDateString();
                    baseGridModelList.Add(baseGridModel);
                }

                switch (item.Status)
                {
                    case OrderStatus.SELLER_CONSIGNED_PART:
                        SellerConsignedPartModel sellerConsignedPartModel = new SellerConsignedPartModel() { Trade = item };
                        baseGridModel.SellerConsignedPartModel.Add(sellerConsignedPartModel);
                        break;
                    case OrderStatus.TRADE_BUYER_SIGNED:
                        TradeBuyerSignedModel tradeBuyerSignedModel = new TradeBuyerSignedModel() { Trade = item };
                        baseGridModel.TradeBuyerSignedModel.Add(tradeBuyerSignedModel);
                        break;
                    case OrderStatus.TRADE_CLOSED:
                        TradeClosedModel tradeClosedModel = new TradeClosedModel() { Trade = item };
                        baseGridModel.TradeClosedModel.Add(tradeClosedModel);
                        break;
                    case OrderStatus.TRADE_CLOSED_BY_TAOBAO:
                        TradeClosedByTaoBaoModel tradeClosedByTaoBaoModel = new TradeClosedByTaoBaoModel() { Trade = item };
                        baseGridModel.TradeClosedByTaoBaoModel.Add(tradeClosedByTaoBaoModel);
                        break;
                    case OrderStatus.TRADE_FINISHED:
                        TradeFinishedModel tradeFinishedModel = new TradeFinishedModel() { Trade = item };
                        baseGridModel.TradeFinishedModel.Add(tradeFinishedModel);
                        break;
                    case OrderStatus.TRADE_NO_CREATE_PAY:
                        TradeNoCreatePayModel tradeNoCreatePayModel = new TradeNoCreatePayModel() { Trade = item };
                        baseGridModel.TradeNoCreatePayModel.Add(tradeNoCreatePayModel);
                        break;
                    case OrderStatus.WAIT_BUYER_CONFIRM_GOODS:
                        WaitBuyerConfirmGoodsModel waitBuyerConfirmGoodsModel = new WaitBuyerConfirmGoodsModel() { Trade = item };
                        baseGridModel.WaitBuyerConfirmGoodsModel.Add(waitBuyerConfirmGoodsModel);
                        break;
                    case OrderStatus.WAIT_BUYER_PAY:
                        WaitBuyerPayModel waitBuyerPayModel = new WaitBuyerPayModel() { Trade = item };
                        baseGridModel.WaitBuyerPayModel.Add(waitBuyerPayModel);
                        break;
                    case OrderStatus.WAIT_SELLER_SEND_GOODS:
                        WaitSellerSendGoodsModel waitSellerSendGoodsModel = new WaitSellerSendGoodsModel() { Trade = item };
                        baseGridModel.WaitSellerSendGoodsModel.Add(waitSellerSendGoodsModel);
                        break;
                }
            });

            return baseGridModelList;
        }

        /// <summary>
        /// 获取满意度视图模型
        /// </summary>
        /// <param name="sessionKey"></param>
        /// <returns></returns>
        public List<ManYiDuViewModel> GetTradeRateViewModel(string sessionKey)
        {
            List<ManYiDuViewModel> manYiDuViewModelList = new List<ManYiDuViewModel>();
            ManYiDuViewModel manYiDuViewModel = new ManYiDuViewModel() { TradeRateList = new List<TradeRate>() };
            var response = TraderateAPI.TradesSoldGetResponse(sessionKey);

            for (int i = 0; i < response.TradeRates.Count; i++)
            {
                string date = Convert.ToDateTime(response.TradeRates[i].Created).ToShortDateString();

                if (manYiDuViewModel.OrderDate != date)
                {
                    manYiDuViewModel = new ManYiDuViewModel()
                    {
                        OrderDate = date,
                        TradeRateList = new List<TradeRate>()
                    };

                    manYiDuViewModel.TradeRateList.Add(response.TradeRates[i]);
                    manYiDuViewModelList.Add(manYiDuViewModel);
                }

                manYiDuViewModel.TradeRateList.Add(response.TradeRates[i]);
            }

            return manYiDuViewModelList;
        }


        /// <summary>
        /// 获取聊天对象视图模型
        /// </summary>
        /// <param name="sessionKey"></param>
        /// <returns></returns>
        public List<ChatpeersEntity> GetChatpeersViewModel(DateTime startTime, DateTime endTime, string sessionKey, string nick)
        {
            List<ChatpeersEntity> chatpeersEntityList = new List<ChatpeersEntity>();
            var nameList = MockTaoBaoServices.SubUserAPI.SellercenterSubusersGetResponse(nick, sessionKey);

            nameList.Subusers.ForEach(subNick =>
            {
                //var chatpeersResponse = WangWangAPI.GetWangwangEserviceChatpeersGetResponse(startTime, endTime, sessionKey, subNick.Nick);
                var chatpeersResponse = MockTaoBaoServices.WangWangAPI.GetWangwangEserviceChatpeersGetResponse();

                ChatpeersEntity chatpeersEntity = new ChatpeersEntity()
                {
                    Employee = new List<EmployeeEntity>()
                };
                EmployeeEntity employee = new EmployeeEntity { BuyerNick = new List<string>() };

                chatpeersResponse.Chatpeers.ForEach(item =>
                {
                    if (chatpeersEntity.Date != item.Date)
                    {
                        chatpeersEntity = new ChatpeersEntity();
                        chatpeersEntity.Date = item.Date;
                        chatpeersEntity.Employee = new List<EmployeeEntity>();

                        employee.UserNick = subNick.Nick;
                        employee.BuyerNick.Add(item.Uid.Replace(ConfigConstant.CnTaoBao, string.Empty));
                        chatpeersEntity.Employee.Add(employee);

                        chatpeersEntityList.Add(chatpeersEntity);
                    }
                    else
                    {
                        employee.BuyerNick.Add(item.Uid.Replace(ConfigConstant.CnTaoBao, string.Empty));
                    }
                });

            });

            return chatpeersEntityList;
        }
    }
}

